package main

import (
	"fmt"
	"strings"
)

func reverseWords(s string) string {
	s = " " + s
	i, j := len(s)-1, len(s)
	var o strings.Builder
	for ; i >= 0; i-- {
		if s[i] == ' ' {
			sub := s[i+1 : j]
			if len(sub) != 0 {
				o.WriteString(sub)
				o.WriteString(" ")
			}
			j = i
		}
	}
	// 去除两边的空格 (只有out末尾末尾有可能有空格，由17行引入)
	out := o.String()
	if out[len(out)-1] == ' ' {
		out = out[:len(out)-1]
	}
	return out
}
func main() {
	fmt.Println(reverseWords("a good   example"))
}
